package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.BJi;
import com.xy.model.Category;
import com.xy.model.Level;
import com.xy.model.Major;
import com.xy.model.Student;
import com.xy.service.StudentService;
import com.xy.service.impl.CategoryServiceImpl;
import com.xy.service.impl.ClassServiceImpl;
import com.xy.service.impl.LevelServiceImpl;
import com.xy.service.impl.MajorServiceImpl;
import com.xy.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		StudentService service = new StudentServiceImpl();
		if ("goadd".equals(request.getParameter("status"))) {
			List<Object> clist = new CategoryServiceImpl().list(Category.class);
			List<Object> mlist = new MajorServiceImpl().list(Major.class);
			List<Object> llist = new LevelServiceImpl().list(Level.class);
			List<Object> blist = new ClassServiceImpl().list(BJi.class);
			request.setAttribute("clist", clist);
			request.setAttribute("mlist", mlist);
			request.setAttribute("llist", llist);
			request.setAttribute("blist", blist);

			request.getRequestDispatcher("/pages/student/student004/add_student.jsp").forward(request, response);

		} else if ("goquery".equals(request.getParameter("status"))) {
			List<Object> clist = new CategoryServiceImpl().list(Category.class);
			List<Object> mlist = new MajorServiceImpl().list(Major.class);
			List<Object> llist = new LevelServiceImpl().list(Level.class);
			List<Object> blist = new ClassServiceImpl().list(BJi.class);
			request.setAttribute("clist", clist);
			request.setAttribute("mlist", mlist);
			request.setAttribute("llist", llist);
			request.setAttribute("blist", blist);
			request.getRequestDispatcher("/pages/student/student004/query_student.jsp").forward(request, response);

		} else if ("goup".equals(request.getParameter("status"))) {
			Integer id = Integer.valueOf(request.getParameter("id"));
			Student stu = (Student) service.get(id);
			List<Object> clist = new CategoryServiceImpl().list(Category.class);
			List<Object> mlist = new MajorServiceImpl().list(Major.class);
			List<Object> blist = new ClassServiceImpl().list(BJi.class);
			request.setAttribute("stu", stu);
			request.setAttribute("clist", clist);
			request.setAttribute("mlist", mlist);
			request.setAttribute("blist", blist);
			request.getRequestDispatcher("/pages/student/student004/modify_student.jsp").forward(request, response);

		} else if ("add".equals(request.getParameter("status"))) {
			Student stu = new Student();
			stu.setNum(request.getParameter("num"));
			stu.setSex(request.getParameter("sex"));
			stu.setSname(request.getParameter("sname"));
			stu.setRemark(request.getParameter("remark"));
			stu.setAddress(request.getParameter("address"));
			stu.setBirthday(request.getParameter("birthday"));
			stu.setbJi((BJi) new ClassServiceImpl().get(Integer.valueOf(request.getParameter("bj"))));
			stu.setNativeplace(request.getParameter("nativeplace"));
			stu.setTel(request.getParameter("tel"));
			stu.setCreateDate(new Date());
			if (service.add(stu)) {
				request.getRequestDispatcher("/studentServlet?status=list&currentPage=0&radio=n").forward(request,
						response);
			} else {
				System.out.println("error");
				request.getRequestDispatcher("/pages/student/student004/add_student.jsp").forward(request, response);
			}

		} else if ("list".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;
			String list = null;
			String num = null;
			String bj = null;
			BJi bji = null;

			PageResults pageResults = null;

			if (request.getParameter("radio") != null) {
				request.getSession().setAttribute("radio", request.getParameter("radio"));
			}

			if ("n".equals(request.getSession().getAttribute("radio"))) {
				if (request.getParameter("list") == null) {
					num = request.getParameter("num") == null ? "%" : request.getParameter("num");
					list = num;
				} else {
					list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
					num = list;
				}
				
				pageResults = service.query(num, currentPage);
				try {
					bji = ((Student) pageResults.getResults().get(0)).getbJi();
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				pageResults.setPageSize(5);
				request.setAttribute("bj", bji);
				request.setAttribute("pageResults", pageResults);
				request.setAttribute("status", "list");
				request.getSession().setAttribute("radio", "n");
				request.setAttribute("list", list);

			} else {
				if (request.getParameter("list") == null) {
					bj = request.getParameter("bj") == null ? "%" : request.getParameter("bj");
					list = bj;
				} else {
					bj = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
					list = bj;
				}
				bji = (BJi) new ClassServiceImpl().get(Integer.valueOf(bj));
				pageResults = service.queryByC(bj, currentPage);
				pageResults.setPageSize(5);
				request.setAttribute("bj", bji);
				request.setAttribute("pageResults", pageResults);
				request.setAttribute("status", "list");
				request.getSession().setAttribute("radio", "c");
				request.setAttribute("list", list);
			}
			
			

			request.getRequestDispatcher("/pages/student/student004/query_result.jsp").forward(request, response);

		} else if ("del".equals(request.getParameter("status"))) {
			Integer id = Integer.valueOf(request.getParameter("id"));
			Student bj = (Student) service.get(id);
			if (service.delete(bj)) {
				request.getRequestDispatcher("/studentServlet?status=list&currentPage=0&radio=n").forward(request,
						response);
			} else {
				System.out.println("error");
				request.getRequestDispatcher("/studentServlet?status=list&currentPage=0&radio=n").forward(request,
						response);
			}
		} else if ("up".equals(request.getParameter("status"))) {
			Integer id = Integer.valueOf(request.getParameter("id"));
			Student stu = (Student) service.get(id);
			stu.setNum(request.getParameter("num"));
			stu.setSex(request.getParameter("sex"));
			stu.setSname(request.getParameter("sname"));
			stu.setRemark(request.getParameter("remark"));
			stu.setAddress(request.getParameter("address"));
			stu.setBirthday(request.getParameter("birthday"));
			stu.setbJi((BJi) new ClassServiceImpl().get(Integer.valueOf(request.getParameter("bj"))));
			stu.setNativeplace(request.getParameter("nativeplace"));
			stu.setTel(request.getParameter("tel"));
			if (service.update(stu)) {
				request.getRequestDispatcher(
						"/studentServlet?status=list&currentPage=0&radio=n&num=" + request.getParameter("num"))
						.forward(request, response);
			} else {
				System.out.println("error");
				request.getRequestDispatcher("/studentServlet?status=list&currentPage=0&radio=n&num=%").forward(request,
						response);
			}
		} else if ("detail".equals(request.getParameter("status"))) {
			Integer id = Integer.valueOf(request.getParameter("id"));
			Student stu = (Student) service.get(id);
			request.setAttribute("stu", stu);
			request.getRequestDispatcher("/pages/student/student004/student_information.jsp").forward(request,
					response);
		}
	}

}
